home *** CD-ROM | disk | FTP | other *** search
/ A.C.E. 2 / ACE CD 2.iso / FILES / UTILS / AMOSPRO6.DMS / in.adf / Procedures / Plasma / _Plasma2.AMOS / _Plasma2.amosSourceCode
Encoding:
AMOS Source Code  |  1992-09-29  |  9.4 KB  |  198 lines

  1. '******************************************************* 
  2. '*                                                     * 
  3. '* AMOS Professional Procedure Library                 * 
  4. '*                                                     * 
  5. '* Procedure: Plasma FX Routine                        * 
  6. '*                                                     * 
  7. '*    Author: Mike Stevens, Immortal Software          * 
  8. '*                                                     * 
  9. '******************************************************* 
  10.  
  11. 'The main procedure to this routine (_PLASMA_MAKE) creates a plasma effect 
  12. 'field by a simple (and rather slow) recursive algorithm. It does this by
  13. 'chopping up the plasma field into cells, modifies the colour of each cell by  
  14. 'a random amount, and then chops up each cell into a further number of cells,
  15. 'modifying each of those, and so on, until we reach the iteration level
  16. 'initially requested.
  17.  
  18. '  The _PLASMA routine is a front-end that does all the housework and  
  19. 'generally makes using the _PLASMA_MAKE routine a lot easier.
  20.  
  21. '  The demo program for this simply calls the _PLASMA procedure with some
  22. 'defaults. The default value is a fairly low level of iteration (I didn't
  23. 'want to bore the user to death first time they ran the program). To get a 
  24. 'better plasma inscrease the ITER parameter in the call to _PLASMA.
  25.  
  26. '  Although this procedure works so slowly, there are a couple of ways of
  27. 'getting around this- first, you can compile it (I haven't actually tried this 
  28. 'yet); and second, you can generate the plasma on a screen, and then save
  29. 'it as an IFF screen, or an AMOS compressed picture bank.
  30.  
  31. '  One more point to note about this routine- to generate the plasma colours,  
  32. 'I used the EdCols routine which is also on this disk. I generated a few 
  33. 'palettes and saved them as IFF palettes on the disk as "PlasmaN_pal.IFF"
  34. 'where N is a number. When run, the program asks you to load one of these. 
  35. 'It uses the _PALETTE_LOAD procedure from the EdCols routine to do this. 
  36.  
  37. Randomize Timer
  38. Screen Open 0,320,256,32,Lowres
  39. Curs Off : Flash Off 
  40. Cls 0
  41. _PNAME$=Fsel$("Plasma*.IFF","","Pick a palette to use...","")
  42. If _PNAME$="" Then Direct 
  43. _PALETTE_LOAD[_PNAME$]
  44. _PLASMA[0,0,256,256,6,1,31,5]
  45. End 
  46. '
  47. Procedure _PLASMA[TLX,TLY,WID,HGT,ITER,CLRMIN,CLRMAX,SPEED]
  48. '
  49. '   Inputs...
  50. '         TLX, TLY - Top left co-ordinates of plasma field on screen.
  51. '         WID, HGT - Width and height of the plasma field (pixels).
  52. '         ITER - Number of iterations over which to calculate the plasma.
  53. '         CLRMIN, CLRMAX - Range of colours to use for plasma. 
  54. '         SPEED - Rate at which to rotate colours. To rotate in opposite 
  55. '            direction to normal, make this number negative. Values closer 
  56. '            to 0 are faster. 0 itself will produce no rotation at all.
  57. '
  58. ' ***********************************
  59. '
  60. ' NB  This procedure is a front end for the _PLASMA_MAKE procedure, which
  61. ' can be quite daunting to use. What this procedure does is to automatically 
  62. ' work out the optimum values to pass to _PLASMA_MAKE to get an effective
  63. ' plasma, and starts the colour cycling to animate the plasma automatically. 
  64. ' It is currently set up for a default cell array size of 2*2, but you can 
  65. ' play around with this to see how it affects the plasma. If you increase
  66. ' the array size, you will usually want to decrease the ITER value.
  67. '
  68.    _PLS_AWID=2 : _PLS_AHGT=2 : Rem ' Default cell array dimensions. 
  69.    _PLS_FWID=1 : _PLS_FHGT=1 : Rem ' Starting values to calculate the optimum field width.
  70.    ' These two loops calculate the optimum field width to pass to the 
  71.    '_PLASMA_MAKE routine. This is based around the idea that the calculation
  72.    'width/height should be a power of the cell array width/height. The actual 
  73.    'display width and height are then affected by the Clip command. 
  74.    Repeat 
  75.       _PLS_FWID=_PLS_FWID*_PLS_AWID
  76.    Until _PLS_FWID>=WID
  77.    Repeat 
  78.       _PLS_FHGT=_PLS_FHGT*_PLS_AHGT
  79.    Until _PLS_FHGT>=HGT
  80.    ' Work out the best RATE to get  
  81.    _PLS_RATE=(CLRMAX-CLRMIN)/ITER
  82.    ' Set up the initial screen state. 
  83.    Cls 0,TLX,TLY To TLX+WID-1,TLY+HGT-1
  84.    ' Restrict graphics to the requested screen area.
  85.    Clip TLX,TLY To TLX+WID-1,TLY+HGT-1
  86.    ' Now work out the actual plasma colours.  
  87.    _PLASMA_MAKE[TLX,TLY,_PLS_FWID,_PLS_FHGT,_PLS_AWID,_PLS_AHGT,ITER,CLRMIN,CLRMAX,_PLS_RATE]
  88.    ' Finally, start them off cycling to get that truly psychedelic effect...
  89.    ' *********   Hey man, like, which way's the moon ?!?  ;-)     *********** 
  90.    If SPEED>0 Then Shift Up SPEED,CLRMIN,CLRMAX,1 Else Shift Down SPEED,CLRMIN,CLRMAX,1
  91.    ' Deactivate clipping before returning...
  92.    Clip 0,0 To Screen Width,Screen Height
  93. End Proc
  94. '
  95. Procedure _PLASMA_MAKE[TLX,TLY,FWID,FHGT,AWID,AHGT,ITER,CLRMIN,CLRMAX,RATE]
  96.    '
  97.    ' INPUTS :-  TLX,TLY - Co-ords of top left corner of plasma. 
  98.    '            FWID,FHGT - (FieldWIDth and FieldHeiGhT) Size of screen area
  99.    '                  used by plasma. For best effects, these should be 
  100.    '                  calculated as powers of AWID and AHGT respectively.     
  101.    '            AWID,AHGT - (ArrayWID and ArrayHeiGhT) Dimensions of the array  
  102.    '                  used to split up a cell into more cells. 2 is the 
  103.    '                  minimum for each. NB the 'array' is not an actual 
  104.    '                  variable, but just a way representing the plasma field. 
  105.    '            ITER - The number of iterations over which to calulate the
  106.    '                  plasma. Appropriate values for this will vary depending 
  107.    '                  upon AWID and AHGT, and there will be an effective
  108.    '                  maximum for every resolution (although it is possible to
  109.    '                  calculate beyond the maximum).
  110.    '            CLRMIN - The lowest colour in the palette to be used for the  
  111.    '                  plasma field. It's best to start this at colour 1,
  112.    '                  since cycling colour 0 (the background) does your eyes
  113.    '                  in. 
  114.    '            CLRMAX - The highest colour in the palette to be used for the 
  115.    '                  plasma field. 
  116.    '            RATE - The maximum amount by which a colour can change (in
  117.    '                  either direction) in any one one iteration. 
  118.    '
  119.    ' *************************************************************
  120.    '
  121.    If ITER<1 Then Pop Proc : Rem ' Is this the last level of recursion ?
  122.    CWID=Max(1,FWID/AWID) : Rem ' (CellWIDth) Width of each cell in pixels 
  123.    CHGT=Max(1,FHGT/AHGT) : Rem ' (CellHeiGhT) Height of each cell in pixels 
  124.    ' This loop splits the field up into an array of cells AHGT�AWID big.
  125.    For Y=0 To AHGT-1
  126.       For X=0 To AWID-1
  127.          ' For each cell, we first get the current colour...
  128.          _PLS_CELLCLR=Point(TLX+X*CWID,TLY+Y*CHGT)
  129.          ' Then we modify it by a random number in the range -RATE to +RATE.
  130.          Add _PLS_CELLCLR,Rnd(RATE*2)-RATE,CLRMIN To CLRMAX
  131.          ' Now fill the cell in, in the new colour. 
  132.          Ink _PLS_CELLCLR
  133.          ' This little If..Then..Else construct is necessitated by the fact 
  134.          'that AMOS' Bar command won't work if the bar size is only 1 pixel 
  135.          'in any direction, so we have to decide whether to use Bar or Draw.
  136.          If CWID>1 and CHGT>1
  137.             Bar TLX+X*CWID,TLY+Y*CHGT To TLX+X*CWID+CWID-1,TLY+Y*CHGT+CHGT-1
  138.          Else 
  139.             Draw TLX+X*CWID,TLY+Y*CHGT To TLX+X*CWID+CWID-1,TLY+Y*CHGT+CHGT-1
  140.          End If 
  141.          ' Finally, we recursively call ourself to further divide and fill
  142.          'this cell.
  143.          _PLASMA_MAKE[TLX+X*CWID,TLY+Y*CHGT,CWID,CHGT,AWID,AHGT,ITER-1,CLRMIN,CLRMAX,RATE]
  144.       Next X
  145.    Next Y
  146.    ' And that's all there is to it! Simple, eh? 
  147. End Proc
  148. '
  149. '
  150. 'This procedure is from the EdCols palette editor unit.
  151. '
  152. Procedure _PALETTE_LOAD[NAME$]
  153. '
  154. '  Inputs... 
  155. '    NAME$ - The filename of the IFF-ILBM type colour palette file to load.
  156. '
  157. ' ************************** 
  158. '
  159. '  User Functions... 
  160. '        Fn _RED - Returns the red component of a colour, given its RGB
  161. '                 value. 
  162. '        Fn _GREEN - As _RED, but returns the green component. 
  163. '        FN _BLUE - As _RED and _GREEN, but returns the blue component.
  164. '
  165. ' ************************** 
  166. '
  167.    Def Fn _RED(C)=(C and $F00)/$100
  168.    Def Fn _GREEN(C)=(C and $F0)/$10
  169.    Def Fn _BLUE(C)=C and $F
  170.    ' Open the file in question. 
  171.    Open In 1,NAME$
  172.    ' Check that it is an IFF type.
  173.    _PLD_BUFFER$=Input$(1,4)
  174.    If _PLD_BUFFER$<>"FORM" Then Close 1 : Error 30
  175.    ' Cjeck that it is an ILBM type. 
  176.    _PLD_BUFFER$=Input$(1,4)
  177.    _PLD_BUFFER$=Input$(1,4)
  178.    If _PLD_BUFFER$<>"ILBM" Then Close 1 : Error 30
  179.    ' Now we have to read each chunk until we find the "CMAP" chunk. 
  180.    _PLD_CHUNK$=Input$(1,4) : Rem  'First chunk's type 
  181.    _PLD_BUFFER$=Input$(1,4) : Rem 'First chunk's length
  182.    While _PLD_CHUNK$<>"CMAP" and Not Eof(1) : Rem 'Go until we find the CMAP or we hit EOF.
  183.       _PLD_CHUNKSIZE=Leek(Varptr(_PLD_BUFFER$)) : Rem 'Turn chunklength into a number variable.  
  184.       Pof(1)=Pof(1)+_PLD_CHUNKSIZE : Rem 'Read past this chunk. 
  185.       _PLD_CHUNK$=Input$(1,4) : Rem  'Read next chunk's type.  
  186.       _PLD_BUFFER$=Input$(1,4) : Rem 'Read next chunk's length. 
  187.    Wend 
  188.    If Eof(1) Then Close 1 : Error 30 : Rem ' If we didn't find the CMAP chunk, then quit. 
  189.    _PLD_CMAPSIZE=Leek(Varptr(_PLD_BUFFER$)) : Rem ' Read in whole palette. 
  190.    For _PLD_INDEX=0 To Min(_PLD_CMAPSIZE/3-1,31)
  191.       _PLD_RED=Asc(Input$(1,1))*$10
  192.       _PLD_GREEN=Asc(Input$(1,1))
  193.       _PLD_BLUE=Asc(Input$(1,1))/$10
  194.       Colour _PLD_INDEX,_PLD_RED+_PLD_GREEN+_PLD_BLUE
  195.    Next _PLD_INDEX
  196.    ' Close the file and end.
  197.    Close 1
  198. End Proc